1687B - Railway System - CodeForces Solution


constructive algorithms graphs greedy interactive sortings *1700

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
 
def query(x):
	print("? " + "".join(x))
	return input().strip()
 
edges = [0 for i in range(m)]
ask = ["0"]*m
for i in range(m):
	ask[i-1] = "0"
	ask[i] = "1"
	edges[i] = int(query(ask))
 
s = sorted((v, i) for i, v in enumerate(edges))
 
c_sum = 0
ask[-1] = "0"
 
for v, s_i in s:
	ask[s_i] = "1"
	q = int(query(ask))
 
	if c_sum + v > q:
		ask[s_i] = "0"
	else:
		c_sum = q
 
print("!", c_sum)

C++ Code:

#include <bits/stdc++.h>
using namespace std; 
typedef long long int ll;
#define MAXN 300010
const ll MOD = 1e9+7;

ll x[MAXN],y[MAXN],a[MAXN],b[MAXN];
vector<int> adj[20000];
ll n,m,k;


void solve() 
{
    cin >> n >> m;

    vector<pair<int,int>> weights;

    
    for (int i = 0; i < m; i++) {
        vector<char> sV(m, '0');
        sV[i] = '1';
        string s(sV.begin(), sV.end());
        cout << "? "<< s << endl;
        int val;
        cin >> val;
        weights.push_back({val,i});
    }
    sort(weights.begin(), weights.end());

    vector<char> resV(m, '0');

    ll currCost = 0;
    for (int i = 0;i < m; i++) {
        resV[weights[i].second] = '1';
        string s(resV.begin(), resV.end());
        cout << "? " << s << endl;
        int val;
        cin >> val;
        if (val - currCost == weights[i].first) {
            currCost = val;
        } else {
            resV[weights[i].second] = '0';
        }
    }


    cout << "! " <<currCost << endl;




}


int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
 
	ll t = 1;
	while (t--) {
        solve();
	}
	return EXIT_SUCCESS;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST